package com.hnkywl.sts.service.kcgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.kcgl.KcpdDao;
import com.hnkywl.sts.dao.kcgl.PdmxDao;
import com.hnkywl.sts.dao.xsgl.KhDao;
import com.hnkywl.sts.entity.organ.Ck;
import com.hnkywl.sts.entity.xsgl.Kcpd;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.entity.xsgl.Pdmx;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.xsgl.KhService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-25
 * Time: 下午2:39
 * To change this template use File | Settings | File Templates.
 */
@Service
public class KcpdService {
    private KcpdDao kcpdDao;
    private PdmxDao pdmxDao;
    private KhDao khDao;
    private BmService bmService;

    //库存盘点的查询
    @Transactional
    public String findKcpdList(Long ckId, String ckMc, int pageNo, int pageSize) {
        Page<Kcpd> kcpdPage = this.kcpdDao.findKcpdList(ckId, ckMc, pageNo, pageSize);
        List<Kcpd> kcpdList = kcpdPage.getResult() == null ? new ArrayList<Kcpd>() : kcpdPage.getResult();
        String kcpdJson = BaseUtils.fetchCollectionToJson(kcpdList, "id", "kcpdbm", "dateCreated", "lastUpdated", "pdck.id", "pdck.mc", "pdyf", "zmje", "spje", "ykje", "yksl", "pdr.xm", "shry.xm", "shrq", "sfsh");
        return "{\"total\":" + kcpdPage.getTotalCount() + ",\"rows\":[" + kcpdJson + "]}";
    }

    /**
     * 查询库存盘点明细
     *
     * @param id 库存盘点id
     * @return
     */
    @Transactional
    public Kcpd findKcpdDetail(Long id) {
        return this.kcpdDao.get(id);
    }

    /**
     * 保存库存盘点信息
     *
     * @param kcpd 库存盘点
     * @return
     */
    @Transactional
    public void saveKcpd(Kcpd kcpd) throws RuntimeException {
        kcpd.setPdrq(new Date());
        kcpd.setPdr(YgService.getCurrentYg());
        kcpd.setCreateUser(YgService.getCurrentYg());
        kcpd.setDateCreated(new Date());
        kcpd.setKcpdbm(this.bmService.getKcpdbm());
        kcpd.setLastUpdated(kcpd.getDateCreated());
        this.kcpdDao.save(kcpd);
    }

    /**
     * 更新库存盘点信息
     *
     * @param kcpd 库存盘点
     * @return
     */
    @Transactional
    public void updateKcpd(Kcpd kcpd) throws RuntimeException {
        Kcpd pd = this.kcpdDao.get(kcpd.getId());
        pd = (Kcpd) ReflectionUtils.getInstance(pd, kcpd);
        pd.setLastUpdated(new Date());
        this.kcpdDao.save(pd);
    }

    /**
     * 删除库存盘点信息
     *
     * @param id 库存盘点id
     * @return
     */
    @Transactional
    public void deleteKcpd(Long id) {
        Kcpd kcpd = kcpdDao.get(id);
        kcpd.setLastUpdated(new Date());
        kcpd.setDeleted(1);
        for (Pdmx pdmx : kcpd.getPdmxList()) {
            pdmx.setDeleted(1);
            this.pdmxDao.save(pdmx);
        }
        this.kcpdDao.save(kcpd);
    }

    @Transactional
    public void shKcpd(Long kcpdId) {
        Kcpd kcpd = kcpdDao.get(kcpdId);
        kcpd.setLastUpdated(new Date());
        kcpd.setShry(YgService.getCurrentYg());
        kcpd.setShrq(new Date());
        kcpd.setSfsh(true);
        for (Pdmx pdmx : kcpd.getPdmxList()) {
            if (pdmx.getYksl() == null) {
                throw new RuntimeException("物资还没有进行实盘！");
            }
            Kh kh = pdmx.getKh();
            kh.setSl(kh.getSl() + pdmx.getYksl());
            kh.setKkdsl(kh.getKkdsl() + pdmx.getYksl());
            this.khDao.save(kh);
        }
        this.kcpdDao.save(kcpd);
    }

    /**
     * 生成盘点明细
     *
     * @param kcpd 库存盘点单
     * @return
     */
    @Transactional
    public void createPdmx(Kcpd kcpd) {
        if (!kcpd.isSfscpdb()) {
            List<Kh> KhList = this.khDao.findKhByCkId(kcpd.getPdck().getId());
            for (Kh t : KhList) {
                Pdmx pdmx = new Pdmx();
                pdmx.setKh(t);
                pdmx.setZmsl(t.getSl());
                pdmx.setWsdj(t.getWsdj());
                pdmx.setKcpd(kcpd);
                pdmx.setSpsl(0d);
                pdmx.setCreateUser(YgService.getCurrentYg());
                pdmx.setDateCreated(new Date());
                pdmx.setLastUpdated(pdmx.getDateCreated());
                this.pdmxDao.save(pdmx);
            }
            kcpd.setSfscpdb(true);
            kcpdDao.save(kcpd);
        }
    }

    /**
     * 保存库存盘点明细
     *
     * @param kcpd 库存盘点
     * @return
     */
    @Transactional
    public void savePdmx(Kcpd kcpd) throws RuntimeException {
        if (kcpd.getPdmxList() != null) {
            for (Pdmx pdmx : kcpd.getPdmxList()) {
                Pdmx pd = this.pdmxDao.get(pdmx.getId());
                pd.setSpsl(pdmx.getSpsl());
                pd.setYksl(pd.getSpsl() - pd.getZmsl());
                pd.setYkje(pd.getYksl() * pd.getWsdj());
                pd.setLastUpdated(new Date());
                this.pdmxDao.save(pd);
            }
        }
    }

    /**
     * 查询库存盘点明细
     *
     * @param kcpdId
     * @return
     */
    public String findPdmxList(Long kcpdId) {
        Gson gson = new Gson();
        List<Map> pdmxList = this.kcpdDao.findPdmxList(kcpdId);
        String result = "{\"rows\":" + gson.toJson(pdmxList) + "}";
        return result;
    }

    /**
     * 查询库存盘点明细
     *
     * @param kh 卡号
     * @return
     */
    public List<Map> findPdmxByKh(Kh kh) {
        return this.kcpdDao.findPdmxByKh(kh);
    }

    /**
     * ajax远程验证
     *
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id, String value, Object field) {
        return this.kcpdDao.checkUnique(id, value, field);
    }

    @Autowired
    public void setKcpdDao(KcpdDao kcpdDao) {
        this.kcpdDao = kcpdDao;
    }

    @Autowired
    public void setPdmxDao(PdmxDao pdmxDao) {
        this.pdmxDao = pdmxDao;
    }

    @Autowired
    public void setKhDao(KhDao khDao) {
        this.khDao = khDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }
}
